﻿Public Class StudentMgm
    Private Sub StudentMgm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Me.StudentTableAdapter1.Fill(Me.ExamDataSet.student)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub FillToolStripButton_Click(sender As Object, e As EventArgs)
        Try
            Me.StudentTableAdapter1.Fill(Me.ExamDataSet.student)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub dgvStudent_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles dgvStudent.DataBindingComplete
        Dim r As DataGridViewRow
        If dgvStudent.RowCount >= 1 Then
            For Each r In dgvStudent.Rows
                r.Cells(12).Value = "编辑"
                r.Cells(12).Tag = r.Cells(0).Value
                r.Cells(13).Value = "删除"
                r.Cells(13).Tag = r.Cells(0).Value
                If r.Cells(6).Value = 0 Then
                    r.Cells(7).Value = "否"
                Else
                    r.Cells(7).Value = "是"
                End If
                If r.Cells(8).Value = 0 Then
                    r.Cells(9).Value = "男"
                Else
                    r.Cells(9).Value = "女"
                End If

            Next r
        End If

    End Sub

    Private Sub dgvStudent_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvStudent.CellMouseClick
        If e.RowIndex > -1 Then
            If e.ColumnIndex > 0 And e.ColumnIndex <= 12 Then
                Dim Id As String = dgvStudent.Rows.Item(e.RowIndex).Cells(0).Value
                Me.StudentTableAdapter1.FillById(Me.updateDataSet.student, Id)
                'dgvStudent.Rows.Item(e.RowIndex)
            ElseIf e.ColumnIndex = 13 Then
                If MsgBox("是否删除该学生？", MsgBoxStyle.YesNo, "警告") = MsgBoxResult.Yes Then
                    Dim returned = Me.StudentTableAdapter1.DeleteQuery(dgvStudent.SelectedCells(0).Tag.ToString())
                    If returned > 0 Then
                        System.Windows.Forms.MessageBox.Show("删除学生信息成功！")
                        Me.StudentTableAdapter1.Fill(Me.ExamDataSet.student)
                    Else
                        System.Windows.Forms.MessageBox.Show("删除学生信息失败！")
                    End If
                End If
            End If

        End If


    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Dim selected As Integer = cbSearchType.SelectedIndex
        Dim content As String = "%" + txtSearchContent.Text + "%"
        Try
            Select Case selected
                Case 0
                    If Not String.IsNullOrWhiteSpace(content) Then

                        Me.StudentTableAdapter1.FillByName(Me.ExamDataSet.student, content)
                    Else
                        Me.StudentTableAdapter1.Fill(Me.ExamDataSet.student)

                    End If
                Case 1
                    If Not String.IsNullOrWhiteSpace(content) Then

                        Me.StudentTableAdapter1.FillByLoginName(Me.ExamDataSet.student, content)
                    Else
                        Me.StudentTableAdapter1.Fill(Me.ExamDataSet.student)

                    End If
                Case 2
                    If Not String.IsNullOrWhiteSpace(content) Then

                        Me.StudentTableAdapter1.FillByCertId((Me.ExamDataSet.student), content)
                    Else
                        Me.StudentTableAdapter1.Fill(Me.ExamDataSet.student)

                    End If
                Case Else
                    Me.StudentTableAdapter1.Fill(Me.ExamDataSet.student)
            End Select
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub txtLoginName_TextChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub lblId_TextChanged(sender As Object, e As EventArgs) Handles lblId.TextChanged
        If lblIsActiveValue.Tag IsNot Nothing Then
            Select Case lblIsActiveValue.Tag.ToString()
                Case "1"
                    chkIsActive.Checked = True
                Case Else
                    chkIsActive.Checked = False
            End Select
        End If

        If lblGenderValue.Tag IsNot Nothing Then
            Select Case lblGenderValue.Tag.ToString()
                Case "1"
                    rbFemale.Checked = True
                    rbMale.Checked = False
                Case Else
                    rbFemale.Checked = False
                    rbMale.Checked = True
            End Select
        End If

    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        clear()

    End Sub

    Private Sub clear()
        Me.lblIsActiveValue.Tag = Nothing
        Me.lblGenderValue.Tag = Nothing
        Me.lblId.Text = Nothing
        Me.txtAge.Text = Nothing
        Me.txtCertId.Text = Nothing
        Me.txtLoginName.Text = Nothing
        Me.txtName.Text = Nothing
        Me.txtPwd.Text = Nothing
        chkIsActive.Checked = False
        rbFemale.Checked = False
        rbMale.Checked = False
    End Sub

    Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
        Dim age As Integer = Integer.Parse(Me.txtAge.Text)
        Dim certId As String = Me.txtCertId.Text
        Dim loginName As String = Me.txtLoginName.Text
        Dim name As String = Me.txtName.Text
        Dim pwd As String = Me.txtPwd.Text

        Dim isActive As Integer = 0
        If Me.chkIsActive.Checked Then
            isActive = 1
        End If
        Dim gender As Integer
        If rbMale.Checked Then
            gender = 0
        ElseIf rbFemale.Checked Then
            gender = 1
        Else
            gender = 0
        End If
        If String.IsNullOrWhiteSpace(lblId.Text) Then
            Dim returned = Me.StudentTableAdapter1.Insert(loginName, pwd, name, age, certId, isActive, Date.Now(), Date.Now(), gender)
            If returned > 0 Then
                System.Windows.Forms.MessageBox.Show("创建学生成功！")
                clear()
                Me.StudentTableAdapter1.Fill(Me.ExamDataSet.student)
            Else
                System.Windows.Forms.MessageBox.Show("创建学生失败请检查数据！")
            End If
        Else
            Dim id As Integer = Integer.Parse(lblId.Text)
            Dim returned = Me.StudentTableAdapter1.UpdateQuery(loginName, pwd, name, age, certId, isActive, Date.Now(), gender, id)
            If returned > 0 Then
                System.Windows.Forms.MessageBox.Show("修改学生信息成功！")
                clear()
                Me.StudentTableAdapter1.Fill(Me.ExamDataSet.student)
            Else
                System.Windows.Forms.MessageBox.Show("修改学生信息失败请检查数据！")
            End If
        End If

    End Sub


    Private Sub StudentMgm_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        End
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs)
        End
    End Sub

    Private Sub QuestionToolStripMenuItem_Click(sender As Object, e As EventArgs)
        Me.Hide()
        SelectQuestions.Show()
    End Sub

    Private Sub ExamMgmToolStripMenuItem_Click(sender As Object, e As EventArgs)
        Me.Hide()
        ExamMgm.Show()
    End Sub


    Private Sub ImportToolStripMenuItem_Click(sender As Object, e As EventArgs)
        StuImportFromExcel()

    End Sub

    Public Sub StuImportFromExcel()
        Dim fdia As OpenFileDialog = New OpenFileDialog()
        fdia.Filter = "Excel 文件（.xls)|*.xls|所有文件|*.*"
        fdia.ShowDialog()
        Dim fileName = fdia.FileName
        Dim line As Integer = 2
        If Not String.IsNullOrEmpty(fileName) Then
            Try
                Dim xlExcel As Excel.Application = New Excel.Application()
                Dim xlBook As Excel.Workbook
                xlBook = xlExcel.Workbooks.Open(fileName)
                xlExcel.Visible = False
                Dim xlSheet As Excel.Worksheet = xlBook.Worksheets(1)

                xlSheet.Activate()
                'Dim dt As ExamDataSet.studentDataTable = New ExamDataSet.studentDataTable()
                For index = 2 To xlSheet.UsedRange.Rows.Count
                    'Dim row As ExamDataSet.studentRow = dt.NewstudentRow()
                    Dim login_name As String = xlSheet.Cells(1)(index).Value
                    Dim pwd As String = xlSheet.Cells(2)(index).Value
                    Dim name As String = xlSheet.Cells(3)(index).Value
                    Dim age As Integer = Integer.Parse(xlSheet.Cells(4)(index).Value)
                    Dim cert_id As String = xlSheet.Cells(5)(index).Value
                    'Dim isActive As String = xlSheet.Cells(6)(index).Value
                    Dim is_active As Integer = 1
                    'If String.Equals(isActive, "是") Then
                    '    is_active = 1
                    'ElseIf String.Equals(isActive, "否") Then
                    '    is_active = 0
                    'End If
                    Dim genderStr As String = xlSheet.Cells(6)(index).Value
                    Dim gender As Integer = 0
                    If String.Equals(genderStr, "男") Then
                        gender = 0
                    ElseIf String.Equals(genderStr, "女") Then
                        gender = 1
                    End If
                    Dim create_time As Date = Date.Now()
                    Dim modify_time As Date = Date.Now()
                    'dt.Rows.Add(row)
                    Me.StudentTableAdapter1.Insert(login_name, pwd, name, age, cert_id, is_active, create_time, modify_time, gender)
                    line = line + 1
                Next
                xlBook.Close()
                xlExcel.Quit()
                xlExcel = Nothing
                'Me.StudentTableAdapter1.Update(dt)
                Me.StudentTableAdapter1.Dispose()
                MessageBox.Show("导入学生成功！")
                Me.StudentTableAdapter1.Fill(Me.ExamDataSet.student)
            Catch ex As System.Exception
                If ex.Message.Contains("certId") Then
                    System.Windows.Forms.MessageBox.Show("导入学生失败！在第" + line.ToString() + "行。身份证重复。")
                Else
                    System.Windows.Forms.MessageBox.Show("导入学生失败！在第" + line.ToString() + "行。错误信息：" + ex.Message)
                End If

            End Try
        End If
    End Sub

    Private Sub CreateNewStuToolStripMenuItem_Click(sender As Object, e As EventArgs)
        CreateNewStu()
    End Sub

    Public Sub CreateNewStu()
        Dim createF As Form = New CreateStu()
        createF.Owner = Me
        createF.ShowDialog()
        refreshData()
    End Sub

    Public Sub refreshData()
        Try
            Me.StudentTableAdapter1.Fill(Me.ExamDataSet.student)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class